package com.odm.communitymanage.base;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.SnackbarUtils;
import com.odm.communitymanage.R;
import com.odm.communitymanage.util.GlideUtils;
import com.odm.communitymanage.widget.AvatarModeUtils;
import com.odm.communitymanage.widget.CommonDialog;

import java.util.ArrayList;
import java.util.List;

import me.yokeyword.fragmentation.SupportFragment;

/**
 * @description: 基类Fragemnt
 * @author: ODM
 * @date: 2020/4/5
 */
public abstract class BaseFragment extends SupportFragment {

    public View mContentView;


    @androidx.annotation.Nullable
    @Override
    public View onCreateView(@androidx.annotation.NonNull LayoutInflater inflater, @androidx.annotation.Nullable ViewGroup container, @androidx.annotation.Nullable Bundle savedInstanceState) {
        mContentView = inflater.inflate(getLayoutId() , container ,false);
        return mContentView;
    }

    @Override
    public void onActivityCreated(@androidx.annotation.Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initViews();
    }

    /**
     * 每次Fragment可见时必定会回调的函数
     */
    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
        configStatusBar();
    }

    public void showSuccessSnack(String msg) {
        //通知用户成功
        SnackbarUtils.with(mContentView)
                .setMessage(msg)
                .setMessageColor(Color.WHITE)
                .setDuration(SnackbarUtils.LENGTH_SHORT)
                .setBgResource(R.color.colorAccent)
                .show();
    }

    /**
     * 更新 用户的头像信息
     */
    public void setNameAvatar(String name , ImageView target){
        if(! "".equals(name)){
            //加载仿钉钉头像
             ViewGroup.LayoutParams para = target.getLayoutParams();
            int w = para.width;
            int h = para.height;
            String firstName = String.valueOf(name.charAt(0));
            List<Object> list = new ArrayList<>();
            list.add(firstName);
            GlideUtils.getInstance().loadBitmap(
                    requireContext(),
                    AvatarModeUtils.getAvatar(list, w , h),
                    target
            );
        }
    }




    /**
     * 显示一个View，隐藏另一个View
     * @param showView
     * @param hideView
     */
    public void showAndHideView (View showView ,View  hideView){
        showView.setVisibility(View.VISIBLE);
        hideView.setVisibility(View.GONE);
    }

    @Override
    public boolean onBackPressedSupport() {
        if(shouldPopUpCloseDialog()){
            CommonDialog dialog = new CommonDialog(requireContext());
            dialog.setContentText("是否确认退出");
            dialog.setOnEnsureClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                    ActivityUtils.finishAllActivities();
                }
            });
            dialog.setOnCancelClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
            dialog.show();
        }else {
            pop();
        }
        return true;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    /**
     * 初始化控件
     */
    public abstract void initViews();

    /**
     * 配置
     */
    public  void configStatusBar(){

    }

    public abstract int getLayoutId();

    public boolean shouldPopUpCloseDialog(){
        return false;
    }

}
